home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / st80_vw.lha / st80_vw / speedMeasurement.st < prev    next >
Text File  |  1993-07-24  |  3KB  |  64 lines

  1. "       NAME            speedMeasurement.st
  2.         AUTHOR          bruce@utafll.uta.edu (Bruce Samuelson)
  3.         FUNCTION        measures speed using Time millisecondsToRun: block
  4.         ST-VERSIONS     ParcPlace VisualWorks 1.0 and ObjectWorks 4.1
  5.         PREREQUISITES   none
  6.         CONFLICTS       none
  7.         DISTRIBUTION    world
  8.         VERSION         1.0
  9.         DATE            July 8, 1993
  10.  
  11. SUMMARY This method in the 'public domain' protocol of BlockClosure
  12. measures the speed for running the raw code inside the block. It is
  13. invoked by evaluating 'aBlock speed'.
  14.  
  15. Bruce Samuelson"!
  16.  
  17. 'From VisualWorks(TM), Release 1.0 of 8 October 1992 on 8 July 1993 at 2:40:42 pm'!
  18.  
  19. !BlockClosure methodsFor: 'public domain'!
  20.  
  21. speed
  22.     "Copyright (c) 1993 by Bruce Samuelson. Released to the public domain.
  23.  
  24.     Measure the time to evaluate the receiver minus the time to evaluate an empty 
  25.     block. Return a string detailing raw execution speed of the code inside the block.
  26.     
  27.     slow        ['hello' , ' world'] speed
  28.     medium    [1+1+1+1+1+1+1+1+1+1+1] speed
  29.     fast            [1+1] speed
  30.     infinite        [] speed
  31.  
  32.     To get accurate measures of raw speed for fast operations such as integer
  33.     addition, the block must have many internal iterations. For example, VisualWorks
  34.     1.0 on an Amax 486/33, 256K cache, 16 MB ram, DOS 6.0, Windows 3.1 yields
  35.  
  36.     1    million adds per second with [1+1]        one addition inside the block
  37.     4-5    million adds per second with [1+1+...+1]    10 additions inside the block
  38.     7    million adds per second with [1+1+...+1]    100 additions inside the block.
  39.  
  40.     The inability to get an accurate measurement with one internal iteration may be
  41.     partly caused by the timeBlock being copying rather than clean. Copying blocks
  42.     evaluate more slowly than the equivalent clean ones."
  43.  
  44.     | minSeconds targetSeconds n emptyBlock timeBlock secondsSelf secondsEmpty t |
  45.     minSeconds := 0.1.
  46.     targetSeconds := 1.0.
  47.     n := 1.
  48.     emptyBlock := [].
  49.     timeBlock := [:bl | (Time millisecondsToRun: [n timesRepeat: bl]) * 0.001].
  50.     ObjectMemory garbageCollect.
  51.     [(t := timeBlock value: self) < minSeconds] whileTrue: [n := n * 2].
  52.     n := (n * targetSeconds / t) rounded.
  53.     ObjectMemory garbageCollect.
  54.     secondsSelf := timeBlock value: self.
  55.     ObjectMemory garbageCollect.
  56.     secondsEmpty := timeBlock value: emptyBlock.
  57.     ^(t := secondsSelf - secondsEmpty) > (secondsSelf * 0.15)
  58.         ifTrue: ['
  59. ' , n printString , ' iterations
  60. ' , t printString , ' seconds (time self - time empty block)
  61. ' , (t * 1.0e6 / n) printString , ' microseconds per iteration
  62. ' , (n / t) printString , ' iterations per second']
  63.         ifFalse: ['The block is too fast to measure this way.']! !
  64.